﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.DAL;
using System.IO;
using System.Web;

namespace PBOffice.BLL
{
    public  class FileOperationBLL
    {
        #region 查询所有的文件类型
        /// <summary>
        /// 查询所有的文件类型
        /// </summary>
        /// <returns></returns>
        public static List<DocumentType> SelectAllDocumentType()
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var dt = from v in myoffice.DocumentTypes
                         select v;
                return dt.ToList();
            }
        } 
        #endregion

        #region 查询文件的详细信息

        /// <summary>
        /// 查询文件夹的详细信息（包括文件夹）
        /// </summary>
        /// <param name="FatherNode">父节点的ID</param>
        /// <returns></returns>
        public static List<v_SelectAllDocumentManage> SelectAllDoc(int FatherNode)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var Doc = from v in myoffice.v_SelectAllDocumentManage
                          where v.FatherNode == FatherNode && v.IsDelete==false
                          select v;
                return Doc.ToList();
            }
        } 
        #endregion

        #region 查询文件编号
        /// <summary>
        /// 查询文件路径，根据文件编号查询
        /// </summary>
        /// <param name="DocumentNo"> 文件编号</param>
        /// <returns></returns>
        public static string SelectDocPathByDocNo(int DocumentNo)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var doc = from v in myoffic.Documents
                           where v.DocNo == DocumentNo
                           select v;
                List<Document> docment = doc.ToList();
                string Path = docment[0].DocPath.ToString();
                return Path;
            }
        } 
        #endregion

        #region 删除到回收站
        /// <summary>
        /// 删除到回收站
        /// </summary>
        /// <param name="DocNo">删除的文件编号</param>
        public static void DeletToRecycleByDocId(int DocNo)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var doc = from v in myoffic.Documents
                          where v.DocNo == DocNo
                          select v;
                Document docment = doc.SingleOrDefault();
                docment.IsDelete = true;
                myoffic.SaveChanges();
            }
        } 
        #endregion

        #region 查询回收站的信息
        /// <summary>
        /// 查询回收站的信息
        /// </summary>
        /// <returns></returns>
        public static List<v_SelectAllDocumentManage> SelectAlllRecycle()
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var doc = from v in myoffic.v_SelectAllDocumentManage
                          where v.IsDelete == true
                          select v;
                return doc.ToList();
            }
        } 
        #endregion

        #region 恢复回收站删除的文件
        /// <summary>
        /// 恢复回收站删除的文件
        /// </summary>
        /// <param name="DocNo">需要恢复的文件的ID</param>
        /// <returns></returns>
        public static bool Upcancel(int DocNo)
        {
            bool b = false;
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var doc = from v in myoffice.Documents
                          where v.DocNo == DocNo
                          select v;
                Document docment = doc.SingleOrDefault();
                docment.IsDelete = false;
                if (myoffice.SaveChanges() > 0)
                {
                    b = true;
                }
                return b;
            }
        } 
        #endregion

        #region 创建新的文件夹
        /// <summary>
        /// 添加新的文件夹
        /// </summary>
        /// <param name="doc">文件夹的信息集合</param>
        /// <param name="serverPath">服务器的物理路径</param>
        /// <param name="userNo">创建人ID</param>
        /// <returns></returns>
        public static bool AddFolder(Document doc,int userNo)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var docment = myoffic.Documents.CreateObject();
                docment.DocId = doc.DocId;//名称
                docment.DocMemo = doc.DocMemo;//备注
                docment.DocCreatDate = DateTime.Now;
                docment.DocPath = doc.DocPath;//路径
                docment.DocumentType = myoffic.DocumentTypes.SingleOrDefault(type => type.DocTypeNo == 1);// 类型
                docment.User = myoffic.Users.SingleOrDefault(user => user.UserNo == userNo);//获取用户
                docment.FatherNode = doc.FatherNode;//父节点ID
                //存在性判断
                var judge = from v in myoffic.Documents
                            where v.DocPath == docment.DocPath
                            select v;
                judge.ToList();
                if (judge.Count()==0)
                {
                    //记录到数据库
                    myoffic.Documents.AddObject(docment);
                    if (myoffic.SaveChanges() > 0)
                        return true;
                    else
                        return false;
                }
                else
                {
                    return false;
                }
            }
        } 
        #endregion

        #region 查询所有的文件信息
        /// <summary>
        /// 查询所有的文件信息(不包括文件夹)
        /// </summary>
        /// <param name="faNode">需要查询的节点</param>
        /// <returns></returns>
        public static List<v_SelectAllDocumentManage> SelectAllFile(int faNode)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var file = from v in myoffic.v_SelectAllDocumentManage
                           where v.DocTypeNo != 1 && v.FatherNode == faNode && v.IsDelete==false
                           select v;
                return file.ToList();
            }
        } 
        #endregion

        #region 查询一个文件的信息
        /// <summary>
        /// 查询一个文件的信息
        /// </summary>
        /// <param name="fileNo">需要查询的文件信息的ID</param>
        /// <returns></returns>
        public static List<v_SelectAllDocumentManage> SelectOneFile(int fileNo)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var files = from v in myoffic.v_SelectAllDocumentManage
                            where v.DocNo == fileNo
                            select v;
                return files.ToList();
            }
        } 
        #endregion

        #region 添加新的文件
        /// <summary>
        /// 添加新的文件。成功返回true
        /// </summary>
        /// <param name="doc">文件信息的实体类</param>
        /// <param name="userID">上传人的ID</param>
        /// <param name="FileType">文件的类型</param>
        ///   <param name="faPath">父节点的路径</param>
        /// <returns></returns>
        public static bool AddFile(Document doc,  int userID, int FileType,string faPath)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var docment = myoffice.Documents.CreateObject();
                docment.DocId = doc.DocId;
                docment.DocMemo = doc.DocMemo;
                docment.DocPath = doc.DocPath.ToLower().Trim();
                docment.DocCreatDate = doc.DocCreatDate;
                docment.DocumentType = myoffice.DocumentTypes.SingleOrDefault(type => type.DocTypeNo == FileType);
                docment.User = myoffice.Users.SingleOrDefault(user => user.UserNo == userID);
                docment.FatherNode = (myoffice.Documents.SingleOrDefault(fa => fa.DocPath == faPath.Trim())).DocNo;
                docment.IsDelete = doc.IsDelete;
                myoffice.Documents.AddObject(docment);
                if (myoffice.SaveChanges() >= 1)
                    return true;
                else
                    return false;
            }
        } 
        #endregion

        #region 修改文件的信息
        /// <summary>
        /// 修改文件的信息
        /// </summary>
        /// <param name="doc">文件的信息集合</param>
        /// <param name="filetype">文件的类型ID</param>
        /// <returns></returns>
        public static bool UpdateFile(Document doc, int filetype, string trueFilename)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var docment = from v in myoffic.Documents
                        where v.DocNo == doc.DocNo
                        select v;
                foreach (var item in docment)
                {
                    item.DocId = doc.DocId;
                    item.DocMemo = doc.DocMemo;
                    item.DocPath = doc.DocPath + trueFilename;
                    item.DocumentType = myoffic.DocumentTypes.SingleOrDefault(type => type.DocTypeNo == filetype);
                    //查询出父节点
                    item.FatherNode = (myoffic.Documents.SingleOrDefault(fa => fa.DocPath == doc.DocPath)).DocNo;
                }
                if (myoffic.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        } 
        #endregion

        #region 查询文件是否包括内容
        /// <summary>
        /// 判断文件是否包括别的内容。包括返回false，不包括返回true
        /// </summary>
        /// <param name="DocNo">需要查询的文件的ID</param>
        /// <returns></returns>
        public static bool SelectFilesSon(int DocNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var file = from v in myoffice.Documents
                           where v.FatherNode == DocNo
                           select v;
                if (file.ToList().Count > 0)
                    return false;
                else
                    return true;
            }
        } 
        #endregion

        #region 删除文件
        /// <summary>
        /// 删除文件。成功返回true
        /// </summary>
        /// <param name="DocNo">需要删除的文件ID</param>
        /// <returns></returns>
        public static bool DeltedFile(int DocNo,string serverPath)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                var file = from v in myoffic.Documents
                           where v.DocNo == DocNo
                           select v;
                //获取文件路径

                var fileManage = file.SingleOrDefault();
                string fileName = fileManage.DocPath.Substring(fileManage.DocPath.LastIndexOf("\\"));
                string filePath = serverPath + fileName;
                try
                {
                    //删除本地
                    File.Delete(filePath);
                    //删除数据库
                    myoffic.Documents.DeleteObject(fileManage);
                    if (myoffic.SaveChanges() > 0)
                        return true;
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        } 
        #endregion

        #region 根据文件名，上传人 查询文件信息
        /// <summary>
        /// 根据文件名，上传人 查询文件信息
        /// </summary>
        /// <param name="AllName">文件的全名</param>
        /// <param name="Name">文件的部分名称</param>
        /// <param name="CreateUser">上传人的名称</param>
        /// <returns></returns>
        public static List<v_SelectAllDocumentManage> SelectAllDoc(string AllName, string Name, string CreateUser)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var file = myoffice.sp_selectfiles(CreateUser, Name, AllName);
                return file.ToList();
            }
        } 
        #endregion


    }
}
